/* **************************************************
 * User Group Manager
 *      author:     Baskin Tapkan - baskint@hotmail.com
 *          written for Code Plex
 *              April - 2008
 *                  http://tapkan.com
 * ****************************************************
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace UserGroupManager
{
    public partial class EditGroup : Form
    {
        string computerName = string.Empty;
        Dictionary<string, string> groupDic = new Dictionary<string, string>();
        string editedGroup = string.Empty;
        string editedDesc = string.Empty;

        public EditGroup(string systemName)
        {
            computerName = systemName;
            InitializeComponent();
            this.Text = string.Format("Edit Groups for {0}", computerName);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void EditGroup_Load(object sender, EventArgs e)
        {
            GroupMgr gm = new GroupMgr();
            gm.ComputerName = computerName;

            groupDic = gm.GetGroupsAndDescriptions();

            UpdateUI();
        }

        private void UpdateUI()
        {
            cmbGroupName.Items.Clear();
            if (groupDic.Count > 0)
            {
                foreach (KeyValuePair<string, string> kvp in groupDic)
                {
                    cmbGroupName.Items.Add(kvp.Key);
                }
            }
        }

        private void cmbGroupName_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtDescription.Text = GetDescription(cmbGroupName.Text);
            btnEdit.Enabled = true;
            editedGroup = cmbGroupName.Text;
            editedDesc = txtDescription.Text;
        }

        private string GetDescription(string grpName)
        {
            string retvalue = string.Empty;
            foreach (KeyValuePair<string, string> kvp in groupDic)
            {
                if (kvp.Key.Equals(grpName))
                    return kvp.Value;
            }
            return retvalue;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            string msg = string.Format("{0} {1} {2} {3}", editedGroup, cmbGroupName.Text, 
                editedDesc, txtDescription.Text);

            GroupMgr gm = new GroupMgr();
            gm.ComputerName = computerName;

            bool res = gm.EditGroup(editedGroup, cmbGroupName.Text,
                editedDesc, txtDescription.Text);

            if (res)
            {
                groupDic.Remove(editedGroup);
                if (!groupDic.ContainsKey(cmbGroupName.Text))
                {
                    groupDic.Add(cmbGroupName.Text, txtDescription.Text);
                }
                UpdateUI();
            }
            staGrpMgr.Items[0].Text = "Group edited successfully";
        }
    }
}